SRC_DIR = src/c++/$(PROJECT_NAME)
PROJECT_NAMES = graph_updater server
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
LIBS = boost_date_time boost_system boost_serialization sqlite3 pthread boost_filesystem boost_thread

CFLAGS = -Wall #-pedantic
PROCESSOR_COUNT = 2

#first part, which executes second by recursive make call
first: server-debug
$(patsubst %,%-debug,$(PROJECT_NAMES)): %-debug:
	# -ggdb
	@make makedirs DIR=build/debug/$* PROJECT_NAME=$* -s 
	@make build/debug/$*/$* \
		  DIR=build/debug \
		  FLAGS='-g -O0' \
		  PROJECT_NAME=$* -j$(PROCESSOR_COUNT)
$(patsubst %,%-release,$(PROJECT_NAMES)): %-release:
	@make makedirs DIR=build/release/$* PROJECT_NAME=$* -s
	@make build/release/$*/$* \
		  DIR=build/release \
    	  FLAGS='-DNDEBUG -02' \
		  PROJECT_NAME=$* -j$(PROCESSOR_COUNT)

#second part
.PHONY: makedirs clean
makedirs:
	@echo "building " $(PROJECT_NAME)
	@mkdir -p $(dir $(subst $(SRC_DIR),$(DIR),$(SRCS))) $(dir $(subst $(SRC_DIR),.depend,$(SRCS)))
clean:
	-@rm -rf build/release/$(PROJECT_NAME) build/debug/$(PROJECT_NAME) .depend

-include $(patsubst $(SRC_DIR)/%.cpp,.depend/%.dep,$(SRCS))

$(DIR)/$(PROJECT_NAME)/$(PROJECT_NAME): $(patsubst $(SRC_DIR)/%.cpp,$(DIR)/$(PROJECT_NAME)/%.o,$(SRCS))
	g++ $^ -o $@ $(patsubst %,-l%,$(LIBS))
$(DIR)/$(PROJECT_NAME)/%.o: $(SRC_DIR)/%.cpp
	g++ $< -c $(CFLAGS) -o $@ $(FLAGS) -MMD -MT $@ -MF .depend/$*.dep -I ./src/c++/include
